﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CoreUtils.WIL
{
    /// <summary>
    /// Logic functions from WIL library
    /// </summary>
    public class Logic
    {
        public static FVIL.Data.CFviImage andOperation(FVIL.Data.CFviImage src1, FVIL.Data.CFviImage src2)
        {
            FVIL.Operation.CFviAndOperation parser = new FVIL.Operation.CFviAndOperation();
            FVIL.Data.CFviImage dst = new FVIL.Data.CFviImage();
            int valid = FVIL.Operation.CFviAndOperation.CheckValidity(src1, src2);
            if (valid != FVIL.ErrorCode._SUCCESS)
                throw new FVIL.CFviException(valid, "FVIL.Operation.CFviAndOperation.CheckValidity");
            parser.SrcImages[0] = src1;
            parser.SrcImages[1] = src2;
            parser.DstImages[0] = dst;
            if (!parser.IsValid())
                parser.Validate();

            parser.Execute();
            return dst;
        }

        public static FVIL.Data.CFviImage andScalar(FVIL.Data.CFviImage src, uint value)
        {
            FVIL.Operation.CFviAndScalar parser = new FVIL.Operation.CFviAndScalar();
            FVIL.Data.CFviImage dst = new FVIL.Data.CFviImage();
            int valid = FVIL.Operation.CFviAndScalar.CheckValidity(src);
            if (valid != FVIL.ErrorCode._SUCCESS)
                throw new FVIL.CFviException(valid, "FVIL.Operation.CFviAndScalar.CheckValidity");
            parser.SrcImages[0] = src;
            parser.DstImages[0] = dst;
            if (!parser.IsValid())
                parser.Validate();
            parser.Value = value;

            parser.Execute();
            return dst;
        }

        public static FVIL.Data.CFviImage nandOperation(FVIL.Data.CFviImage src1, FVIL.Data.CFviImage src2)
        {
            FVIL.Operation.CFviNandOperation parser = new FVIL.Operation.CFviNandOperation();
            FVIL.Data.CFviImage dst = new FVIL.Data.CFviImage();
            int valid = FVIL.Operation.CFviNandOperation.CheckValidity(src1, src2);
            if (valid != FVIL.ErrorCode._SUCCESS)
                throw new FVIL.CFviException(valid, "FVIL.Operation.CFviNandOperation.CheckValidity");
            parser.SrcImages[0] = src1;
            parser.SrcImages[1] = src2;
            parser.DstImages[0] = dst;
            if (!parser.IsValid())
                parser.Validate();

            parser.Execute();
            return dst;
        }

        public static FVIL.Data.CFviImage nandScalar(FVIL.Data.CFviImage src, uint value)
        {
            FVIL.Operation.CFviNandScalar parser = new FVIL.Operation.CFviNandScalar();
            FVIL.Data.CFviImage dst = new FVIL.Data.CFviImage();
            int valid = FVIL.Operation.CFviNandScalar.CheckValidity(src);
            if (valid != FVIL.ErrorCode._SUCCESS)
                throw new FVIL.CFviException(valid, "FVIL.Operation.CFviNandScalar.CheckValidity");
            parser.SrcImages[0] = src;
            parser.DstImages[0] = dst;
            if (!parser.IsValid())
                parser.Validate();
            parser.Value = value;
            parser.Execute();
            return dst;
        }

        public static FVIL.Data.CFviImage orOperation(FVIL.Data.CFviImage src1, FVIL.Data.CFviImage src2)
        {
            FVIL.Operation.CFviOrOperation parser = new FVIL.Operation.CFviOrOperation();
            FVIL.Data.CFviImage dst = new FVIL.Data.CFviImage();
            int valid = FVIL.Operation.CFviOrOperation.CheckValidity(src1, src2);
            if (valid != FVIL.ErrorCode._SUCCESS)
                throw new FVIL.CFviException(valid, "FVIL.Operation.CFviOrOperation.CheckValidity");
            parser.SrcImages[0] = src1;
            parser.SrcImages[1] = src2;
            parser.DstImages[0] = dst;
            if (!parser.IsValid())
                parser.Validate();
            parser.Execute();
            return dst;
        }

        public static FVIL.Data.CFviImage orScalar(FVIL.Data.CFviImage src, uint value)
        {
            FVIL.Operation.CFviOrScalar parser = new FVIL.Operation.CFviOrScalar();
            FVIL.Data.CFviImage dst = new FVIL.Data.CFviImage();
            int valid = FVIL.Operation.CFviOrScalar.CheckValidity(src);
            if (valid != FVIL.ErrorCode._SUCCESS)
                throw new FVIL.CFviException(valid, "FVIL.Operation.CFviOrScalar.CheckValidity");
            parser.SrcImages[0] = src;
            parser.DstImages[0] = dst;
            if (!parser.IsValid())
                parser.Validate();
            parser.Value = value;
            parser.Execute();
            return dst;
        }

        public static FVIL.Data.CFviImage xorOperation(FVIL.Data.CFviImage src1, FVIL.Data.CFviImage src2)
        {
            FVIL.Operation.CFviXorOperation parser = new FVIL.Operation.CFviXorOperation();
            FVIL.Data.CFviImage dst = new FVIL.Data.CFviImage();
            int valid = FVIL.Operation.CFviXorOperation.CheckValidity(src1, src2);
            if (valid != FVIL.ErrorCode._SUCCESS)
                throw new FVIL.CFviException(valid, "FVIL.Operation.CFviXorOperation.CheckValidity");
            parser.SrcImages[0] = src1;
            parser.SrcImages[1] = src2;
            parser.DstImages[0] = dst;
            if (!parser.IsValid())
                parser.Validate();
            parser.Execute();
            return dst;
        }

        public static FVIL.Data.CFviImage xorScalar(FVIL.Data.CFviImage src, uint value)
        {
            FVIL.Operation.CFviXorScalar parser = new FVIL.Operation.CFviXorScalar();
            FVIL.Data.CFviImage dst = new FVIL.Data.CFviImage();
            int valid = FVIL.Operation.CFviXorScalar.CheckValidity(src);
            if (valid != FVIL.ErrorCode._SUCCESS)
                throw new FVIL.CFviException(valid, "FVIL.Operation.CFviXorScalar.CheckValidity");
            parser.SrcImages[0] = src;
            parser.DstImages[0] = dst;
            if (!parser.IsValid())
                parser.Validate();
            parser.Value = value;
            parser.Execute();
            return dst;
        }

        public static FVIL.Data.CFviImage notOperation(FVIL.Data.CFviImage src)
        {
            FVIL.Operation.CFviNotOperation parser = new FVIL.Operation.CFviNotOperation();
            FVIL.Data.CFviImage dst = new FVIL.Data.CFviImage();
            int valid = FVIL.Operation.CFviNotOperation.CheckValidity(src);
            if (valid != FVIL.ErrorCode._SUCCESS)
                throw new FVIL.CFviException(valid, "FVIL.Operation.CFviNotOperation.CheckValidity");
            parser.SrcImages[0] = src;
            parser.DstImages[0] = dst;
            if (!parser.IsValid())
                parser.Validate();
            parser.Execute();
            return dst;
        }

    }
}
